

#ifndef _ACCEPTOR_H_
#define _ACCEPTOR_H_

#include "Channel.hpp"
#include "Socket.hpp"
#include "noncopyable.hpp"
#include <functional>

namespace zephyr {

class EventLoop;
class InetAddress;

class Acceptor : noncopyable {
  public:
    using NewConnectionCallback = std::function<void(int sockfd, const InetAddress &)>;

    Acceptor(EventLoop *loop, const InetAddress &addr, bool reuseport);
    ~Acceptor();

    // 设置新连接的回调函数
    void setNewConnectionCallback(const NewConnectionCallback &callback) {
        m_newconnectioncb = callback;
    }

    bool listening() const { return m_listening; }
    // 开启监听
    void listen();

  private:
    // 处理用户连接
    void handleRead();

  private:
    EventLoop *m_loop;
    Socket m_acceptSocket; //  封装的套接字描述符
    Channel m_acceptChannel; // 专门处理
    NewConnectionCallback m_newconnectioncb; // 新连接的回调函数
    bool m_listening; // 是在监听
};

}

#endif // _ACCEPTOR_H_